10+5
10-5
4*6
16/4
2^8
2*5-(2^5)/8
2 == 2 # is it equal?
3 > 2 # is it larger?
3 < 1 # is it smaller?
4 != 0 # is it not equal?
x <- 1
y <- 2
str1 <- "hello world"
print( str1 )
x; y; str1
z <- x+y
z
z = z + 1
z
ls()
#or
objects()
rm(z) #remove
# save.image() # save variables in the workspace
#q() #quits the session
# load( ".RData" ) # load the workspace
z
ls()
sqrt(1024) # square root
log(4) # Ln
mean(c(100,50,35)) # mean
exp(2) #e^2
round( exp(2), digits=1 ) # most built-in functions have options
?round # documentation
help(round)
mySEM <- function(x){
return(sd(x)/sqrt(length(x)))
}
example_list <- c(1,5,8,2,4,5,6,7,44,1,5,6,7,8,6,8,9)
mySEM(example_list)
getwd() # get working directory
dir <- getwd()
setwd("/home/jovyan/work/data/") # set working directory
getwd()
list.files(path = ".")
setwd("/home/jovyan/work") # set working directory
getwd()
current_dir="."
list.files(path = current_dir)
myCofV(example_list)
source("my-functions.R") # run from script
myCofV(example_list)
class( 2 ) # find the object type of v1
class( c(1,5,7) )
mode("Hello") # and similarly
mode(2==2)
mode(TRUE)
x <- c(1, 2, 3) # Combine Values into a Vector or List
mean(x)
min(x)
max(x)
s <- c( min(x),max(x) )
s
v1 <- c(1,2,3)
c1 <- c( "a", "b", "c", "d" )
v1 * 3
toupper( c1 )
testVector<-c(1,2,3,4,5,6,7,8,9)
m1 <- matrix( testVector, nrow=3, ncol=3 ) # matrix: table of n rows and m columns, only one type of data allowed and equal length required
colnames(m1)<-c('1','2','3')
rownames(m1)<-c('A','B','C')
m1
cbind(m1,"4"=c(1,2,3))
rbind(m1,"D"=c(6,5,4))
var1 <- c( "John", "Paul", "George", "Ringo" )
var2 <- c( 123456, 234567, 345678, 456789 )
var3 <- c( 8.5, 9.0, 5.0, 6.75 )
classroom <- data.frame( ID=var2, Score=var3 ) #data.frame: table of n rows and c columns, different types per column allowed
rownames( classroom ) <- var1
classroom
classroom[1]
classroom$ID # each column can be accessed with its name
classroom[,1]
classroom[1,]
classroom[1,2]
classroom[classroom$ID==234567,]
classroom["Paul",]
classroom["Paul","Score"]
RECORDS <- data.frame(
ID=seq(from=1, to=40, by=1),
Sex=c(rep("Male",times=20),rep("Female",times=20)),
Age=round(rnorm(40, mean=55, sd=5),digits=0),
Score=c(rnorm(20, mean=70, sd=10),rnorm(20, mean=20, sd=15))
)
RECORDS
# 1 Get a new table just for women
# 2 Select only the indivuduals with score higher than the mean score
# 3 Select only the men with a score higher than 70 and age lower than 55
# 4 Order all individuals by score